package com.summer;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Controller
public class FileController {
    @GetMapping("/toUpload")
    public String toUpload(){
        return "upload";
    }
    @PostMapping("/uploadFile")
    public String uploadFile(MultipartFile[] fileUpload, Model model)
    {
        //默认文件上传成功，并返回状态信息
        model.addAttribute("uploadStatus","上传成功");
        for(MultipartFile file:fileUpload){
            //获取文件名以及后缀名
            String fileName = file.getOriginalFilename();
            //重新生成文件名（根据具体情况生成对应文件名）
            fileName = UUID.randomUUID()+"_"+fileName;
            //指定上传文件本地储存目录，不存在则需要提前创建
            String dirPath="F:file(dirPath)";
            File filePath = new File(dirPath);
            if(!filePath.exists()){
                filePath.mkdirs();
            }
            try{
                file.transferTo(new File(dirPath+fileName));
            }catch (Exception e){
                e.printStackTrace();
                //上传失败，返回失败信息
                model.addAttribute("uploadStatus","上传失败"+e.getMessage());
            }
        }
        //携带上传状态信息回调到文件上传页面
        return "upload";
    }

}
